<?php
/**
 * @Author: kaihui.wang
 * @Contact  hpuwang@gmail.com
 * @Version: 1.0.0
 * @Date: 2020/10/9 10:51
 */

namespace Eduxplus\CoreBundle\Service;

use Eduxplus\CoreBundle\Entity\BaseDraft;
use Eduxplus\CoreBundle\Lib\Base\AdminBaseService;
use Eduxplus\CoreBundle\Lib\Service\HelperService;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;

class DraftService extends AdminBaseService
{
    private $paginator;

    public function __construct(HelperService $helperService, PaginatorInterface $paginator)
    {
        $this->helperService = $helperService;    
        $this->paginator = $paginator;
    }

    public function getList($request, $page, $pageSize, $params)
    {
        $uid = $params["uid"];
        $type = $params["type"];
        $sql = $this->getFormatRequestSql($request);
        $dql = "SELECT a FROM Core:BaseDraft a " . $sql . " WHERE a.uid=:uid and a.type=:type ORDER BY a.id DESC";
        $em = $this->getDoctrine()->getManager();
        $em = $this->db()->disableSoftDeleteable($em);
        $query = $em->createQuery($dql);
        $query = $query->setParameters(["uid"=>$uid, "type"=>$type]);
        $pagination = $this->paginator->paginate(
            $query,
            $page,
            $pageSize
        );

        $items = $pagination->getItems();
        $itemsArr = [];
        if ($items) {
            foreach ($items as $v) {
                $vArr =  $this->toArray($v);
                $itemsArr[] = $vArr;
            }
        }
        return [$pagination, $itemsArr];
    }

    public function add($uid, $type, $content){
        $version = "1";

        $sql = "SELECT a FROM Core:BaseDraft a where a.uid=:uid and a.type=:type ORDER BY a.version DESC";
        $info = $this->db()->fetchOneHard($sql, ["uid" => $uid, "type"=>$type]);
        if($info){
            $version = $info['version']+1;
        }

        $model = new BaseDraft();
        $model->setUid($uid);
        $model->setType($type);
        $model->setVersion($version);
        $model->setContent($content);
        $this->db()->save($model);
        return $version;
    }


    public function getById($id){
        $sql = "SELECT a FROM Core:BaseDraft a where a.id=:id";
        $info = $this->db()->fetchOneHard($sql, ["id" => $id]);
        return $info;
    }

    public function del($id)
    {
        $sql = "SELECT a FROM Core:BaseDraft a WHERE a.id=:id";
        $model = $this->db()->fetchOne($sql, ['id' => $id], 1);
        return $this->db()->delete($model);
    }


    /**
     * 假删
     */
    public function del4Complete(Request $request, $uid){
        $version = $request->get("draft_version");
        $type = $request->get("draft_type");
        if($version && $type){
            $this->delByVersionAndType($version, $type, $uid);
        }
    }

    public function delByVersionAndType($version, $type, $uid){
        $sql = "SELECT a FROM Core:BaseDraft a WHERE a.version=:version AND a.type=:type AND a.uid=:uid";
        $models = $this->db()->fetchAll($sql, ["version" => $version, "type"=>$type, "uid"=>$uid], 1);
        if($models){
            $this->db()->delete($models);
        }
    }

    public function updateByVersion($version, $content, $uid){
        $sql = "SELECT a FROM Core:BaseDraft a where a.version =:version AND a.uid=:uid ORDER BY a.id DESC";
        $model = $this->db()->fetchOne($sql, ["version" => $version,"uid"=>$uid], 1);
        $model->setContent($content);
        return $this->db()->save($model);
    }


    public function getLatestByVersion($version, $uid){
        $sql = "SELECT a FROM Core:BaseDraft a where a.version =:version AND a.uid=:uid  ORDER BY a.id DESC";
        return $this->db()->fetchOne($sql, ["version" => $version, "uid"=>$uid]);
    }

    public function getLatestByType($type){
        $sql = "SELECT a FROM Core:BaseDraft a where a.type =:type ORDER BY a.id DESC";
        return $this->db()->fetchOne($sql, ["type" => $type]);
    }

}
